package com.qianclass.talklive.app.utils;

import android.content.Context;
import android.os.Looper;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.request.BaseRequest;
import com.lzy.okserver.download.DownloadManager;
import com.lzy.okserver.download.DownloadService;
import com.orhanobut.logger.Logger;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by admin on 2017/3/30.
 * <p>
 * 更新时间 2017/3/30
 * 更新描述 ${TODO}
 */

public class DownLoadMusic {
    private DownloadManager downloadManager;
    private Context context;


    public DownLoadMusic(Context context) {
        this.context=context;

    }

    public void downMusic(final String url, final String filename){

    new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            downloadManager = DownloadService.getDownloadManager();


            OkGo.get(url)
                    .tag(context)
                    .execute(new FileCallback(filename) {
                        @Override
                        public void onSuccess(File file, Call call, Response response) {
                            Logger.i("mp3下载完成");
                        }
                        @Override
                        public void onBefore(BaseRequest request) {
                        }
                        @Override
                        public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
                        }
                    });

        }
    }).start();

    }
}
